package main;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.GridBagLayout;
import javax.swing.JTextField;
import java.awt.GridBagConstraints;
import javax.swing.JLabel;
import java.awt.Insets;
import java.awt.Component;
import javax.swing.Box;
import java.awt.FlowLayout;
import javax.swing.JButton;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.aptech.mwg.etown.pojo.Account;
import com.aptech.mwg.etown.pojo.exception.InsertDBException;
import com.aptech.mwg.etown.pojo.exception.RetrievingDBException;
import com.aptech.mwg.etown.pojo.exception.SelectDBException;
import com.aptech.mwg.etown.pojo.exception.UpdateDBException;
import com.aptech.mwg.etown.server.business.AccountManager;
import com.aptech.mwg.etown.server.dao.impl.AccountDAOImpl;

public class Login extends JFrame {

	private JPanel contentPane;
	private JTextField txtTxtusers;
	private JTextField txtTxtpass;
	private Component horizontalGlue;
	private Component horizontalGlue_1;
	private Component verticalGlue;
	private JPanel panel;
	private JButton btnLogin;
	private Component horizontalStrut;
	private Component horizontalStrut_1;
	private JButton btnExit;
	private Component horizontalStrut_2;
	private Component horizontalStrut_3;
	private Component verticalGlue_1;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Login frame = new Login();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Login() {
		setTitle("Login window");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 136);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		GridBagLayout gbl_contentPane = new GridBagLayout();
		gbl_contentPane.columnWidths = new int[]{0, 0, 0, 0, 0};
		gbl_contentPane.rowHeights = new int[]{0, 0, 0, 26, 0, 0};
		gbl_contentPane.columnWeights = new double[]{0.0, 0.0, 1.0, 0.0, Double.MIN_VALUE};
		gbl_contentPane.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		contentPane.setLayout(gbl_contentPane);
		
		verticalGlue = Box.createVerticalGlue();
		GridBagConstraints gbc_verticalGlue = new GridBagConstraints();
		gbc_verticalGlue.insets = new Insets(0, 0, 5, 5);
		gbc_verticalGlue.gridx = 2;
		gbc_verticalGlue.gridy = 0;
		contentPane.add(verticalGlue, gbc_verticalGlue);
		
		horizontalGlue = Box.createHorizontalGlue();
		GridBagConstraints gbc_horizontalGlue = new GridBagConstraints();
		gbc_horizontalGlue.insets = new Insets(0, 0, 5, 5);
		gbc_horizontalGlue.gridx = 0;
		gbc_horizontalGlue.gridy = 1;
		contentPane.add(horizontalGlue, gbc_horizontalGlue);
		
		JLabel lblUsername = new JLabel("UserName:  ");
		GridBagConstraints gbc_lblUsername = new GridBagConstraints();
		gbc_lblUsername.anchor = GridBagConstraints.EAST;
		gbc_lblUsername.insets = new Insets(0, 0, 5, 5);
		gbc_lblUsername.gridx = 1;
		gbc_lblUsername.gridy = 1;
		contentPane.add(lblUsername, gbc_lblUsername);
		
		txtTxtusers = new JTextField();
		txtTxtusers.setText("");
		GridBagConstraints gbc_txtTxtusers = new GridBagConstraints();
		gbc_txtTxtusers.insets = new Insets(0, 0, 5, 5);
		gbc_txtTxtusers.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtTxtusers.gridx = 2;
		gbc_txtTxtusers.gridy = 1;
		contentPane.add(txtTxtusers, gbc_txtTxtusers);
		txtTxtusers.setColumns(10);
		
		horizontalGlue_1 = Box.createHorizontalGlue();
		GridBagConstraints gbc_horizontalGlue_1 = new GridBagConstraints();
		gbc_horizontalGlue_1.insets = new Insets(0, 0, 5, 0);
		gbc_horizontalGlue_1.gridx = 3;
		gbc_horizontalGlue_1.gridy = 1;
		contentPane.add(horizontalGlue_1, gbc_horizontalGlue_1);
		
		JLabel lblPassword = new JLabel("Password: ");
		GridBagConstraints gbc_lblPassword = new GridBagConstraints();
		gbc_lblPassword.anchor = GridBagConstraints.EAST;
		gbc_lblPassword.insets = new Insets(0, 0, 5, 5);
		gbc_lblPassword.gridx = 1;
		gbc_lblPassword.gridy = 2;
		contentPane.add(lblPassword, gbc_lblPassword);
		
		txtTxtpass = new JTextField();
		txtTxtpass.setText("");
		GridBagConstraints gbc_txtTxtpass = new GridBagConstraints();
		gbc_txtTxtpass.insets = new Insets(0, 0, 5, 5);
		gbc_txtTxtpass.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtTxtpass.gridx = 2;
		gbc_txtTxtpass.gridy = 2;
		contentPane.add(txtTxtpass, gbc_txtTxtpass);
		txtTxtpass.setColumns(10);
		
		panel = new JPanel();
		GridBagConstraints gbc_panel = new GridBagConstraints();
		gbc_panel.gridwidth = 2;
		gbc_panel.insets = new Insets(0, 0, 5, 5);
		gbc_panel.fill = GridBagConstraints.BOTH;
		gbc_panel.gridx = 1;
		gbc_panel.gridy = 3;
		contentPane.add(panel, gbc_panel);
		panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		
		horizontalStrut_1 = Box.createHorizontalStrut(20);
		panel.add(horizontalStrut_1);
		
		btnLogin = new JButton("Login");
		btnLogin.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				
				AccountManager accmanager = new AccountManager();
				
				try {
					boolean result = accmanager.CheckAccount(txtTxtusers.getText(), txtTxtpass.getText());
					if ( result == true) {
						
						System.out.println("Login Succes!!");
					}else{
						System.out.println("Login fail !!!");
					}
				} catch (SelectDBException e1) {
					System.err.println("Login Fail" + e1.getMessage());
					e1.printStackTrace();
				}
			}
		});
		panel.add(btnLogin);
		
		horizontalStrut = Box.createHorizontalStrut(20);
		panel.add(horizontalStrut);
		
		horizontalStrut_3 = Box.createHorizontalStrut(20);
		panel.add(horizontalStrut_3);
		
		btnExit = new JButton("Exit");
		btnExit.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				System.exit(0);
			}
		});
		panel.add(btnExit);
		
		horizontalStrut_2 = Box.createHorizontalStrut(20);
		panel.add(horizontalStrut_2);
		
		verticalGlue_1 = Box.createVerticalGlue();
		GridBagConstraints gbc_verticalGlue_1 = new GridBagConstraints();
		gbc_verticalGlue_1.insets = new Insets(0, 0, 0, 5);
		gbc_verticalGlue_1.gridx = 2;
		gbc_verticalGlue_1.gridy = 4;
		contentPane.add(verticalGlue_1, gbc_verticalGlue_1);
	}
	

}
